package com.yspa.yspa.util;

import java.util.HashSet;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.yspa.yspa.service.YspaInterfaceManager;
public class CountLineListener implements HttpSessionListener {
	private static YspaInterfaceManager yspaInterfaceManager;
	public void sessionCreated(HttpSessionEvent event) { 
        System.out.println("创建session......"); 
        HttpSession session = event.getSession();
        ServletContext application = session.getServletContext();
        
        // 在application范围由一个HashSet集保存所有的session
        HashSet sessions = (HashSet) application.getAttribute("sessions");
        if (sessions == null) {
               sessions = new HashSet();
               application.setAttribute("sessions", sessions);
        }
        
        // 新创建的session均添加到HashSet集中
        sessions.add(session);
        System.out.println("我们现在有："+sessions.size()+"人---------------------");
         
    } 
 
    /************
     * 销毁session时调用
     */ 
    public void sessionDestroyed(HttpSessionEvent event) { 
    	 HttpSession session = event.getSession();
         ServletContext application = session.getServletContext();
         HashSet sessions = (HashSet) application.getAttribute("sessions");
         String userno =  (String) session.getAttribute("userno");
         yspaInterfaceManager.deleteLoginUser(userno);
         // 销毁的session均从HashSet集中移除
         sessions.remove(session);
         System.out.println("你走的了我们还有："+sessions.size()+"人---------------------");
    }

	public YspaInterfaceManager getYspaInterfaceManager() {
		return yspaInterfaceManager;
	}

	public void setYspaInterfaceManager(YspaInterfaceManager yspaInterfaceManager) {
		this.yspaInterfaceManager = yspaInterfaceManager;
	} 
    
}
